<?php
/**
 * 华强宝处理
 *
 * @category 
 * @package 
 * @author chenping<chenping@shopex.cn>
 * @version $Id: Z
 */
class erpapi_hqepay_request_hqepay extends erpapi_hqepay_request_abstract
{
    /**
     * 订阅华强宝
     *
     * @return void
     * @author 
     **/
    public function pub($sdf,$queue=false)
    {
        
        $args = func_get_args();array_pop($args);
        $_in_mq = $this->__caller->caller_into_mq('hqepay_pub','hqepay',$this->__channelObj->channel['hqepay_id'],$args,$queue);
        if ($_in_mq) {
            return $this->succ('成功放入队列');
        }

        $params = array(
            'company_code'       => trim($sdf['logi_type']),
            'company_name'       => $sdf['logi_name'],
            'logistic_code_list' => $sdf['logi_no'],
            'node_type'          => 'hqepay',
            'to_node_id'         => '1227722633',
        );

        $title = '添加物流订阅['.$sdf['logi_no'].']';

        $title = sprintf('添加物流订阅[%s]',$sdf['logi_no']);

        $callback = array(
            'class'  =>get_class($this),
            'method' => 'callback',
            'params' => array(
                'obj_bn' => $sdf['delivery_bn'],
            ),
        );
      
        return $this->__caller->call(SHOP_LOGISTICS_PUB, $params, $callback, $title,10,$sdf['delivery_bn']);
    }

    /**
     * 华强宝绑定
     *
     * @return void
     * @author 
     **/
    public function bind()
    {
        $params = array(
            'app'           => 'app.applyNodeBind',
            'node_id'       => base_shopnode::node_id('ome'),
            'from_certi_id' => base_certificate::certi_id(),
            'callback'      => '',
            'sess_callback' => '',
            'api_url'       => kernel::base_url(1).kernel::url_prefix().'/api',
            'node_type'     => 'hqepay',
            'to_node'       => '1227722633',
            'shop_name'     => '物流跟踪',
            "api_key"       => "1236217",
            "api_secret"    => "cf98e49d-9ebe-43cb-a690-ad96295b3457",
        );

        $params['certi_ac']=$this->_gen_bind_sign($params);

        $title = '华强宝订阅';

        $callback = array();

        $result = $this->__caller->call(SHOP_LOGISTICS_BIND, $params, $callback, $title,4);

        //$response = json_decode($result['response'],true);

        if ($result['res'] == 'succ' || $result['msg']['errorDescription'] == '绑定关系已存在,不需要重复绑定' ) {
            return true;
        } else {
            return false;
        }
    }

    private function _gen_bind_sign($params)
    {
        $token = base_certificate::token();

        ksort($params);
        $str = '';
        foreach ($params as $key =>$value) {
            $str .= $value;
        }

        $sign = md5($str.$token);

        return $sign;
    }

    /**
     * 物流查询
     *
     * @return void
     * @author
     **/
    public function query($sdf)
    {
        $params = array(
            'to_node_id'    => self::_TO_NODE_ID,
            'tid'           => $sdf['order_bn'],
            'company_code'  => $sdf['logi_code'],
            'company_name'  => $sdf['company_name'],
            'logistic_code' => $sdf['logi_no'],
        );

        $title = sprintf('查询物流信息[%s]',$params['logistic_code']);

        return $this->__caller->call(LOGISTICS_TRACE_DETAIL_GET,$params,array(),$title,5,$params['logistic_code']);
    }
}